package com.shiningstars.general;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Calendar;
import java.util.List;
import java.util.Set;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.TextView;

import com.shiningstars.database.DatabaseHandler;
import com.shiningstars.service.ErrorService;
import com.shiningstars.valuebeans.Error;

public class Utilities implements Constants {
	public static String getDate(int date, int month, int year) {

		StringBuilder dateString = new StringBuilder();

		if (date >= 10) {
			dateString.append(date);
		} else {
			dateString.append(ZERO + date);
		}

		dateString.append(SPACE);
		dateString.append(HYPHEN);
		dateString.append(SPACE);
		dateString.append(getMonth(month));
		dateString.append(SPACE);
		dateString.append(HYPHEN);
		dateString.append(SPACE);
		dateString.append(year);

		return dateString.toString();
	}

	public static String getMonth(int month) {
		switch (month) {
		case 0:
			return "Jan";
		case 1:
			return "Feb";
		case 2:
			return "Mar";
		case 3:
			return "Apr";
		case 4:
			return "May";
		case 5:
			return "Jun";
		case 6:
			return "Jul";
		case 7:
			return "Aug";
		case 8:
			return "Sep";
		case 9:
			return "Oct";
		case 10:
			return "Nov";
		case 11:
			return "Dec";
		default:
			return null;
		}
	}

	public static boolean isBlankString(String str) {
		boolean isBlankString = false;

		if (null == str) {
			isBlankString = true;
		} else if (str.trim().length() == 0) {
			isBlankString = true;
		}

		return isBlankString;
	}

	@SuppressWarnings("rawtypes")
	public static boolean isBlankList(List list) {
		boolean isBlankList = false;

		if (null == list) {
			isBlankList = true;
		} else if (list.size() == 0) {
			isBlankList = true;
		}

		return isBlankList;
	}

	public static void setSharedPreference(Context context, String key, String value) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(SSP, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putString(key, value);
		editor.commit();
	}

	public static void setSharedPreference(Context context, String key, Set<String> values) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(SSP, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putStringSet(key, values);
		editor.commit();
	}

	public static void removeSharedPreference(Context context, String key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(SSP, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		if (null == key) {
			editor.clear();
		} else {
			editor.remove(key);
		}
		editor.commit();
	}

	public static String getSharedPreference(Context context, String key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(SSP, Context.MODE_PRIVATE);
		return sharedPreferences.getString(key, null);
	}

	public static Set<String> getSharedPreferenceSet(Context context, String key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(SSP, Context.MODE_PRIVATE);
		return sharedPreferences.getStringSet(key, null);
	}

	public static int getCenterId(String centerName) {
		if ("Vile Parle".equals(centerName)) {
			return 1;
		} else if ("Borivali".equals(centerName)) {
			return 2;
		} else if ("Hyderabad".equals(centerName)) {
			return 3;
		} else if ("Dadar".equals(centerName)) {
			return 4;
		} else if ("Thane".equals(centerName)) {
			return 5;
		} else if ("Mira Road".equals(centerName)) {
			return 6;
		} else if ("Pune".equals(centerName)) {
			return 7;
		}

		return 0;
	}

	public static String getStackTrace(Exception e) {
		StringWriter sw = new StringWriter();
		e.printStackTrace(new PrintWriter(sw));
		return sw.toString();
	}

	public static void logError(Context context, String className, Exception e) {
		String mobileNumber = getSharedPreference(context, MOBILE_NUMBER);
		String imeiNumber = getSharedPreference(context, IMEI_NUMBER);

		Error error = new Error();
		error.setMobileNumber(mobileNumber);
		error.setImeiNumber(imeiNumber);
		error.setTag(className);
		error.setDescription(getStackTrace(e));
		error.setDate(Calendar.getInstance().getTimeInMillis());

		DatabaseHandler databaseHandler = DatabaseHandler.getInstance(context);
		databaseHandler.logError(error);

		if (!isRunning(context, ErrorService.class.getName())) {
			context.startService(new Intent(context, ErrorService.class));
		}
	}

	public static void logError(Context context, String className, Exception e, String request, String response) {
		String mobileNumber = getSharedPreference(context, MOBILE_NUMBER);
		String imeiNumber = getSharedPreference(context, IMEI_NUMBER);

		Error error = new Error();
		error.setMobileNumber(mobileNumber);
		error.setImeiNumber(imeiNumber);
		error.setTag(className);
		error.setDescription(getStackTrace(e) + NEW_LINE + NEW_LINE + REQUEST + request + NEW_LINE + NEW_LINE
				+ RESPONSE + response);
		error.setDate(Calendar.getInstance().getTimeInMillis());

		DatabaseHandler databaseHandler = DatabaseHandler.getInstance(context);
		databaseHandler.logError(error);

		if (!isRunning(context, ErrorService.class.getName())) {
			context.startService(new Intent(context, ErrorService.class));
		}
	}

	public static void setRatingDescription(TextView textViewRatingResponse, float rating) {
		if (rating == 0) {
			textViewRatingResponse.setText(NO_RESPONSE);
		} else if (rating > 0 && rating <= 1) {
			textViewRatingResponse.setText(POOR);
		} else if (rating > 1 && rating <= 2) {
			textViewRatingResponse.setText(BELOW_AVERAGE);
		} else if (rating > 2 && rating <= 3) {
			textViewRatingResponse.setText(AVERAGE);
		} else if (rating > 3 && rating <= 4) {
			textViewRatingResponse.setText(GOOD);
		} else if (rating > 4 && rating <= 5) {
			textViewRatingResponse.setText(EXCELLENT);
		}
	}

	public static boolean isRunning(Context context, String serviceName) {
		ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
			if (serviceName.equals(service.service.getClassName())) {
				return true;
			}
		}
		return false;
	}

	public static boolean isAppRunning(Context context) {
		ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> runningTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

		if (runningTasks.get(0).topActivity.getPackageName().toString()
				.equalsIgnoreCase(context.getPackageName().toString())) {
			return true;
		}

		return false;
	}

	public static void showNotification(String message) {

	}
}